package com.rocketmq.util;

import java.io.UnsupportedEncodingException;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;
import com.rocketmq.plugins.MqConfig;
import com.rocketmq.plugins.RocketMqProducerPlugin;

/**
 * RocketMQ工具类，用于发送和接收消息
 */
public class RocketMqUtil {

    private static DefaultMQProducer producer = RocketMqProducerPlugin.getProducer();

    private static final String TAG = "TagA";

    public static void sendMessage(String topic, String tags, String message) {
        SendResult sendResult = null;
        try {
            try {
                Message msg = new Message(topic, tags, message.getBytes(RemotingHelper.DEFAULT_CHARSET));
                sendResult = producer.send(msg);
            } catch (MQClientException e) {
                e.printStackTrace();
            } catch (RemotingException e) {
                e.printStackTrace();
            } catch (MQBrokerException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.err.println("sendResult.getMsgId()======:{}" + sendResult.getMsgId());
    }

    public static void sendMessage(String message) {
        sendMessage(MqConfig.TOPIC, TAG, message);
    }

}
